#include "wuziqi.h"
#include "ui_wuziqi.h"
#include <QPainter>
#include <QVector>
#include <QDebug>
#include <QString>
#include<QMessageBox>

wuziqi::wuziqi(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::wuziqi)
{
    ui->setupUi(this);
    resize((CHESS_COLUMES + 1)*RECT_WIDTH  ,(CHESS_ROWS + 1)*RECT_HEIGHT);
    this->setGeometry(QRect(580,150,800,800));
    b_black=true;
}

wuziqi::~wuziqi()
{
    delete ui;
}

void wuziqi::paintEvent(QPaintEvent *event){
    //所有的绘图事件集合
    drawchessboard();
    drawhandchess();
    drawchessitem();
    update();//页面更新


}

void wuziqi::drawchessboard(){
    QPainter painters(this);
    //画棋盘
    //外墙
    painters.setPen(Qt::black);//设置画笔颜色
    painters.setBrush(Qt::gray);//设置填充颜色
    painters.drawRect(0,0,800,800);

    //内墙
    painters.setPen(Qt::black);//设置画笔颜色
    painters.setBrush(Qt::yellow);//设置填充颜色
    painters.drawRect(25,25,750,750);

    //画格子
    painters.setPen(Qt::black);//设置画笔颜色
    for(float i=1.5;i<16;i++){
        painters.drawLine(25,i*50,775,i*50);//画横线
        painters.drawLine(i*50,25,i*50,775);//画竖线

    }
}

void wuziqi::drawhandchess(){
    //画鼠标棋子
    QPainter painters(this);

    //使棋子的边线透明
    painters.setPen(QPen(QColor(Qt::transparent)));

    //根据棋子bool值来判断颜色
    if(b_black){
        painters.setBrush(Qt::black);
    }
    else{
        painters.setBrush(Qt::white);
    }
                        //接受鼠标位置
    painters.drawEllipse(mapFromGlobal(QCursor::pos()),20,20);//圆心为鼠标点击处
}

void wuziqi::drawchessitem(){
    //画棋盘棋子
    //同鼠标棋子
    QPainter painters(this);
    //使棋子的边线透明
    painters.setPen(QPen(QColor(Qt::transparent)));

    //遍历棋子
    for(int i=0;i<p_item.size();i++){
        item it = p_item[i];//当前棋子的样式位置赋给棋子这个类

        if(it._black){
            painters.setBrush(Qt::black);
        }
        else{
            painters.setBrush(Qt::white);
        }
        drawchessatpoint(painters,it._pt);
    }
}

void wuziqi::drawchessatpoint(QPainter&painters,QPoint &point){
    //获取棋子的摆放位置
    int x=(point.x()-25)/25;
    if(x%2==0){
        x=x/2;
    }
    else{
        x=(x+1)/2;
    }
    int y=(point.y()-25)/25;
    if(y%2==0){
        y=y/2;
    }
    else{
        y=(y+1)/2;
    }

    QPoint ptcenter((x+0.5)*50,(y+0.5)*50);//获取圆心
    painters.drawEllipse(ptcenter,20,20);//画棋子
}
void wuziqi::mousePressEvent(QMouseEvent *event){
    //获取鼠标点击位置

    qDebug()<<event->pos()<<endl;
    QPoint pt;//定义点位
    pt.setX((event->pos().x())/50);
    pt.setY((event->pos().y())/50);

    //判断棋子是否存在
    for(int i=0;i<p_item.size();i++){
        p_item[i]._pt.setX(p_item[i]._pt.x()/50);
        p_item[i]._pt.setY(p_item[i]._pt.y()/50);
        item it= p_item[i];
        if(it._pt == pt){
            return;                 //存在则跳过
        }
    }
    item it(pt,b_black);
    p_item.append(it);            //不存在则落子

    //核心算法，判断是否五连（判断应在落子后）
    int lu=countitem(it,QPoint(-1,-1));
    int l=countitem(it,QPoint(-1,0));
    int ld=countitem(it,QPoint(-1,1));
    int d=countitem(it,QPoint(0,1));
    int rd=countitem(it,QPoint(1,1));
    int r=countitem(it,QPoint(1,0));
    int ru=countitem(it,QPoint(1,-1));
    int u=countitem(it,QPoint(0,-1));
    if ( (l + r) >= 4 ||
         (u + d) >= 4 ||
         (lu + rd) >= 4 ||
         (ld + ru) >= 4 )
    {
        QString str = b_black?"黑棋胜利":"白棋胜利";
        QMessageBox::information(NULL,  "游戏结束",str, QMessageBox::Yes );
        //清空棋盘
        p_item.clear();

        return;
    }
    b_black=!b_black;//改变棋子颜色
}

int wuziqi::countitem(item it, QPoint pt){
    int count=0;
    it._pt +=pt;
    while (p_item.contains(it)){
        count++;
        it._pt +=pt;
    }
    return count;
}
